var dateTargetFoundry = 0,
    dateTargetMachining = 0,
    $datePicker = $('.form_date'),
    date = new Date(),
    th,
    url = '/NEPCockpit',
    tabId = window.location.hash;

//定义背景大小与颜色
var setBackground = function () {
    var width = $(window).width();
    var height = $(window).height();
    var $bg = $('.com-bg');
    var bodyHeight = parseInt($bg.css('height'));
    // console.log('height is: ' + height);
    // console.log('bodyHeight is: ' + bodyHeight);
    $bg.css('width', width);
    //如果内容高度高于浏览器页面高度
    if (bodyHeight > height) {
        $bg.css('height', bodyHeight);
    } else {
        $bg.css('height', height);
    }
    if (width < 350) {
        $('.com-deviation').html('Dev.')
    }
    return width;
};

//判断数字函数
var is_numberic = function (val) {
    var result = false;
    var pattern = new RegExp("^ *\\-{0,1} *\\d+(\\.\\d+){0,1} *$");
    var valType = typeof val;
    if (valType === 'string' || valType === 'number') {
        result = pattern.test(val)
    }
    return result;
};

//定义所有表格的数据结构
var libAssembly = [
    {
        $elm: $("#Target_Shift1_Q_Assembly"),
        dbId: 'q_target_1',
        flag: 1
    },
    {
        $elm: $("#Current_Shift1_Q_Assembly"),
        dbId: 'q_current_1',
        flag: 2,
        ida: 'q_current_1',
        idb: 'q_planned_1'
    },
    {
        $elm: $("#Forecast_Shift1_Q_Assembly"),
        dbId: 'q_forecast_1',
        flag: 1
    },
    {
        $elm: $("#Planned_Shift1_Q_Assembly"),
        dbId: 'q_planned_1',
        flag: 1
    },
    {
        $elm: $("#Target_Shift1_P_Assembly"),
        dbId: 'p_target_1',
        flag: 1
    },
    {
        $elm: $("#Current_Shift1_P_Assembly"),
        dbId: 'p_current_1',
        flag: 2,
        ida: 'p_current_1',
        idb: 'p_planned_1'
    },
    {
        $elm: $("#Forecast_Shift1_P_Assembly"),
        dbId: 'p_forecast_1',
        flag: 1
    },
    {
        $elm: $("#Planned_Shift1_P_Assembly"),
        dbId: 'p_planned_1',
        flag: 1
    },
    {
        $elm: $("#Target_Shift2_Q_Assembly"),
        dbId: 'q_target_2',
        flag: 1
    },
    {
        $elm: $("#Current_Shift2_Q_Assembly"),
        dbId: 'q_current_2',
        flag: 2,
        ida: 'q_current_2',
        idb: 'q_planned_2'
    },
    {
        $elm: $("#Forecast_Shift2_Q_Assembly"),
        dbId: 'q_forecast_2',
        flag: 1
    },
    {
        $elm: $("#Planned_Shift2_Q_Assembly"),
        dbId: 'q_planned_2',
        flag: 1
    },
    {
        $elm: $("#Target_Shift2_P_Assembly"),
        dbId: 'p_target_2',
        flag: 1
    },
    {
        $elm: $("#Current_Shift2_P_Assembly"),
        dbId: 'p_current_2',
        flag: 2,
        ida: 'p_current_2',
        idb: 'p_planned_2'
    },
    {
        $elm: $("#Forecast_Shift2_P_Assembly"),
        dbId: 'p_forecast_2',
        flag: 1
    },
    {
        $elm: $("#Planned_Shift2_P_Assembly"),
        dbId: 'p_planned_2',
        flag: 1
    },
    {
        $elm: $("#Assembly_Shift1"),
        ida: 'shift_1_start_time',
        idb: 'shift_1_end_time',
        flag: 3
    },
    {
        $elm: $("#Assembly_Shift2"),
        ida: 'shift_2_start_time',
        idb: 'shift_2_end_time',
        flag: 3
    }
];
var libHVB = [
    {
        $elm: $("#Target_DoubleModule_HVB_Shift1"),
        dbId: 'dm_target_1',
        flag: 1
    },
    {
        $elm: $("#Current_DoubleModule_HVB_Shift1"),
        dbId: 'dm_current_1',
        flag: 2,
        ida: 'dm_current_1',
        idb: 'dm_planned_1'
    },
    {
        $elm: $("#Forecast_DoubleModule_HVB_Shift1"),
        dbId: 'dm_forecast_1',
        flag: 1
    },
    {
        $elm: $("#Planned_DoubleModule_HVB_Shift1"),
        dbId: 'dm_planned_1',
        flag: 1
    },
    {
        $elm: $("#Target_Battery_HVB_Shift1"),
        dbId: 'battery_target_1',
        flag: 1
    },
    {
        $elm: $("#Current_Battery_HVB_Shift1"),
        dbId: 'battery_current_1',
        flag: 2,
        ida: 'battery_current_1',
        idb: 'battery_planned_1'
    },
    {
        $elm: $("#Forecast_Battery_HVB_Shift1"),
        dbId: 'battery_forecast_1',
        flag: 1
    },
    {
        $elm: $("#Planned_Battery_HVB_Shift1"),
        dbId: 'battery_planned_1',
        flag: 1
    },
    {
        $elm: $("#Target_DoubleModule_HVB_Shift2"),
        dbId: 'dm_target_2',
        flag: 1
    },
    {
        $elm: $("#Current_DoubleModule_HVB_Shift2"),
        dbId: 'dm_current_2',
        flag: 2,
        ida: 'dm_current_2',
        idb: 'dm_planned_2'
    },
    {
        $elm: $("#Forecast_DoubleModule_HVB_Shift2"),
        dbId: 'dm_forecast_2',
        flag: 1
    },
    {
        $elm: $("#Planned_DoubleModule_HVB_Shift2"),
        dbId: 'dm_planned_2',
        flag: 1
    },
    {
        $elm: $("#Target_Battery_HVB_Shift2"),
        dbId: 'battery_target_2',
        flag: 1
    },
    {
        $elm: $("#Current_Battery_HVB_Shift2"),
        dbId: 'battery_current_2',
        flag: 2,
        ida: 'battery_current_2',
        idb: 'battery_planned_2'
    },
    {
        $elm: $("#Forecast_Battery_HVB_Shift2"),
        dbId: 'battery_forecast_2',
        flag: 1
    },
    {
        $elm: $("#Planned_Battery_HVB_Shift2"),
        dbId: 'battery_planned_2',
        flag: 1
    },
    {
        $elm: $("#HVB_Shift1"),
        ida: 'shift_1_start_time',
        idb: 'shift_1_end_time',
        flag: 3
    },
    {
        $elm: $("#HVB_Shift2"),
        ida: 'shift_2_start_time',
        idb: 'shift_2_end_time',
        flag: 3
    }
];
var libMachiningG6 = [
    {
        $elm: $("#Target_Shift1_Input_Machining_G6"),
        dbId: 'TargetInput1',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift1_Input_Machining_G6"),
        dbId: 'CurrentInput1',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentInput1',
        idb: 'PlannedInput1'
    },
    {
        $elm: $("#Forecast_Shift1_Input_Machining_G6"),
        dbId: 'ForecastInput1',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastInput1',
        idb: 'TargetInput1'
    },
    {
        $elm: $("#Planned_Shift1_Input_Machining_G6"),
        dbId: 'PlannedInput1',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift1_Input_Machining_G6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentInput1',
        idb: 'PlannedInput1'
    },
    {
        $elm: $("#Target_Shift1_Output_Machining_G6"),
        dbId: 'TargetOutput1',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift1_Output_Machining_G6"),
        dbId: 'CurrentOutput1',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentOutput1',
        idb: 'PlannedOutput1',
        ide: $('#Rate_Shift1_Machining_G6')
    },
    {
        $elm: $("#Forecast_Shift1_Output_Machining_G6"),
        dbId: 'ForecastOutput1',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastOutput1',
        idb: 'TargetOutput1'
    },
    {
        $elm: $("#Planned_Shift1_Output_Machining_G6"),
        dbId: 'PlannedOutput1',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift1_Output_Machining_G6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentOutput1',
        idb: 'PlannedOutput1'
    },
    {
        $elm: $("#Target_Shift2_Input_Machining_G6"),
        dbId: 'TargetInput2',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift2_Input_Machining_G6"),
        dbId: 'CurrentInput2',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentInput2',
        idb: 'PlannedInput2'
    },
    {
        $elm: $("#Forecast_Shift2_Input_Machining_G6"),
        dbId: 'ForecastInput2',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastInput2',
        idb: 'TargetInput2'
    },
    {
        $elm: $("#Planned_Shift2_Input_Machining_G6"),
        dbId: 'PlannedInput2',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift2_Input_Machining_G6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentInput2',
        idb: 'PlannedInput2'
    },
    {
        $elm: $("#Target_Shift2_Output_Machining_G6"),
        dbId: 'TargetOutput2',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift2_Output_Machining_G6"),
        dbId: 'CurrentOutput2',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentOutput2',
        idb: 'PlannedOutput2',
        ide: $('#Rate_Shift2_Machining_G6')
    },
    {
        $elm: $("#Forecast_Shift2_Output_Machining_G6"),
        dbId: 'ForecastOutput2',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastOutput2',
        idb: 'TargetOutput2'
    },
    {
        $elm: $("#Planned_Shift2_Output_Machining_G6"),
        dbId: 'PlannedOutput2',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift2_Output_Machining_G6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentOutput2',
        idb: 'PlannedOutput2'
    },
    {
        $elm: $("#Target_Shift3_Input_Machining_G6"),
        dbId: 'TargetInput3',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift3_Input_Machining_G6"),
        dbId: 'CurrentInput3',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentInput3',
        idb: 'PlannedInput3'
    },
    {
        $elm: $("#Forecast_Shift3_Input_Machining_G6"),
        dbId: 'ForecastInput3',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastInput3',
        idb: 'TargetInput3'
    },
    {
        $elm: $("#Planned_Shift3_Input_Machining_G6"),
        dbId: 'PlannedInput3',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift3_Input_Machining_G6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentInput3',
        idb: 'PlannedInput3'
    },
    {
        $elm: $("#Target_Shift3_Output_Machining_G6"),
        dbId: 'TargetOutput3',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift3_Output_Machining_G6"),
        dbId: 'CurrentOutput3',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentOutput3',
        idb: 'PlannedOutput3',
        ide: $('#Rate_Shift3_Machining_G6')
    },
    {
        $elm: $("#Forecast_Shift3_Output_Machining_G6"),
        dbId: 'ForecastOutput3',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastOutput3',
        idb: 'TargetOutput3'
    },
    {
        $elm: $("#Planned_Shift3_Output_Machining_G6"),
        dbId: 'PlannedOutput3',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift3_Output_Machining_G6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentOutput3',
        idb: 'PlannedOutput3'
    }
];
var libMachiningZ6 = [
    {
        $elm: $("#Target_Shift1_Input_Machining_Z6"),
        dbId: 'TargetInput1',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift1_Input_Machining_Z6"),
        dbId: 'CurrentInput1',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentInput1',
        idb: 'PlannedInput1'
    },
    {
        $elm: $("#Forecast_Shift1_Input_Machining_Z6"),
        dbId: 'ForecastInput1',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastInput1',
        idb: 'TargetInput1'
    },
    {
        $elm: $("#Planned_Shift1_Input_Machining_Z6"),
        dbId: 'PlannedInput1',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift1_Input_Machining_Z6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentInput1',
        idb: 'PlannedInput1'
    },
    {
        $elm: $("#Target_Shift1_Output_Machining_Z6"),
        dbId: 'TargetOutput1',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift1_Output_Machining_Z6"),
        dbId: 'CurrentOutput1',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentOutput1',
        idb: 'PlannedOutput1',
        ide: $('#Rate_Shift1_Machining_Z6')
    },
    {
        $elm: $("#Forecast_Shift1_Output_Machining_Z6"),
        dbId: 'ForecastOutput1',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastOutput1',
        idb: 'TargetOutput1'
    },
    {
        $elm: $("#Planned_Shift1_Output_Machining_Z6"),
        dbId: 'PlannedOutput1',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift1_Output_Machining_Z6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentOutput1',
        idb: 'PlannedOutput1'
    },
    {
        $elm: $("#Target_Shift2_Input_Machining_Z6"),
        dbId: 'TargetInput2',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift2_Input_Machining_Z6"),
        dbId: 'CurrentInput2',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentInput2',
        idb: 'PlannedInput2'
    },
    {
        $elm: $("#Forecast_Shift2_Input_Machining_Z6"),
        dbId: 'ForecastInput2',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastInput2',
        idb: 'TargetInput2'
    },
    {
        $elm: $("#Planned_Shift2_Input_Machining_Z6"),
        dbId: 'PlannedInput2',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift2_Input_Machining_Z6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentInput2',
        idb: 'PlannedInput2'
    },
    {
        $elm: $("#Target_Shift2_Output_Machining_Z6"),
        dbId: 'TargetOutput2',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift2_Output_Machining_Z6"),
        dbId: 'CurrentOutput2',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentOutput2',
        idb: 'PlannedOutput2',
        ide: $('#Rate_Shift2_Machining_Z6')
    },
    {
        $elm: $("#Forecast_Shift2_Output_Machining_Z6"),
        dbId: 'ForecastOutput2',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastOutput2',
        idb: 'TargetOutput2'
    },
    {
        $elm: $("#Planned_Shift2_Output_Machining_Z6"),
        dbId: 'PlannedOutput2',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift2_Output_Machining_Z6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentOutput2',
        idb: 'PlannedOutput2'
    },
    {
        $elm: $("#Target_Shift3_Input_Machining_Z6"),
        dbId: 'TargetInput3',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift3_Input_Machining_Z6"),
        dbId: 'CurrentInput3',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentInput3',
        idb: 'PlannedInput3'
    },
    {
        $elm: $("#Forecast_Shift3_Input_Machining_Z6"),
        dbId: 'ForecastInput3',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastInput3',
        idb: 'TargetInput3'
    },
    {
        $elm: $("#Planned_Shift3_Input_Machining_Z6"),
        dbId: 'PlannedInput3',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift3_Input_Machining_Z6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentInput3',
        idb: 'PlannedInput3'
    },
    {
        $elm: $("#Target_Shift3_Output_Machining_Z6"),
        dbId: 'TargetOutput3',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Current_Shift3_Output_Machining_Z6"),
        dbId: 'CurrentOutput3',
        flag: 3,
        threshold: 'none',
        ida: 'CurrentOutput3',
        idb: 'PlannedOutput3',
        ide: $('#Rate_Shift3_Machining_Z6')
    },
    {
        $elm: $("#Forecast_Shift3_Output_Machining_Z6"),
        dbId: 'ForecastOutput3',
        flag: 3,
        threshold: 'none',
        ida: 'ForecastOutput3',
        idb: 'TargetOutput3'
    },
    {
        $elm: $("#Planned_Shift3_Output_Machining_Z6"),
        dbId: 'PlannedOutput3',
        flag: 1,
        threshold: 'none'
    },
    {
        $elm: $("#Deviation_Shift3_Output_Machining_Z6"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'CurrentOutput3',
        idb: 'PlannedOutput3'
    }
];
var libFoundryRTB2 = [
    {
        $elm: $("#Target_RTB2_Total_Today"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '0',
        idc: 'today'
    },
    {
        $elm: $("#Current_RTB2_Total_Today"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '1',
        idc: 'today',
        ide: $('.com-rtb2Rate')
    },
    {
        $elm: $("#Forecast_RTB2_Total_Today"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '2',
        idc: 'today'
    },
    {
        $elm: $("#Planned_RTB2_Total_Today"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '3',
        idc: 'today'
    },
    {
        $elm: $("#Deviation_RTB2_Total_Today"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '4',
        idc: 'today'
    },

    {
        $elm: $("#Target_RTB2_Total_Week"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '0',
        idc: 'current_week'
    },
    {
        $elm: $("#Current_RTB2_Total_Week"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '1',
        idc: 'current_week'
    },
    {
        $elm: $("#Forecast_RTB2_Total_Week"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '2',
        idc: 'current_week'
    },
    {
        $elm: $("#Planned_RTB2_Total_Week"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '3',
        idc: 'current_week'
    },
    {
        $elm: $("#Deviation_RTB2_Total_Week"),
        dbId: '',
        flag: 5,
        threshold: 'none',
        ida: '',
        idb: '4',
        idc: 'current_week'
    },

    {
        $elm: $("#Target_RTB2_B38A_Today"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B38A',
        idb: '0',
        idc: 'today'
    },
    {
        $elm: $("#Current_RTB2_B38A_Today"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'B38A',
        idb: '1',
        idc: 'today'
    },
    {
        $elm: $("#Forecast_RTB2_B38A_Today"),
        dbId: '',
        flag: 3,
        threshold: 'none',
        ida: 'B38A',
        idb: '2',
        idc: 'today'
    },
    {
        $elm: $("#Planned_RTB2_B38A_Today"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B38A',
        idb: '3',
        idc: 'today'
    },
    {
        $elm: $("#Deviation_RTB2_B38A_Today"),
        dbId: '',
        flag: 4,
        threshold: 'none',
        ida: 'B38A',
        idb: '4',
        idc: 'today'
    },
    {
        $elm: $("#Target_RTB2_B38A_Week"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B38A',
        idb: '0',
        idc: 'current_week'
    },
    {
        $elm: $("#Current_RTB2_B38A_Week"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'B38A',
        idb: '1',
        idc: 'current_week'
    },
    {
        $elm: $("#Forecast_RTB2_B38A_Week"),
        dbId: '',
        flag: 3,
        threshold: 'none',
        ida: 'B38A',
        idb: '2',
        idc: 'current_week'
    },
    {
        $elm: $("#Planned_RTB2_B38A_Week"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B38A',
        idb: '3',
        idc: 'current_week'
    },
    {
        $elm: $("#Deviation_RTB2_B38A_Week"),
        dbId: '',
        flag: 4,
        threshold: 'none',
        ida: 'B38A',
        idb: '4',
        idc: 'current_week'
    },
    {
        $elm: $("#Target_RTB2_B38B_Today"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B38B',
        idb: '0',
        idc: 'today'
    },
    {
        $elm: $("#Current_RTB2_B38B_Today"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'B38B',
        idb: '1',
        idc: 'today'
    },
    {
        $elm: $("#Forecast_RTB2_B38B_Today"),
        dbId: '',
        flag: 3,
        threshold: 'none',
        ida: 'B38B',
        idb: '2',
        idc: 'today'
    },
    {
        $elm: $("#Planned_RTB2_B38B_Today"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B38B',
        idb: '3',
        idc: 'today'
    },
    {
        $elm: $("#Deviation_RTB2_B38B_Today"),
        dbId: '',
        flag: 4,
        threshold: 'none',
        ida: 'B38B',
        idb: '4',
        idc: 'today'
    },
    {
        $elm: $("#Target_RTB2_B38B_Week"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B38B',
        idb: '0',
        idc: 'current_week'
    },
    {
        $elm: $("#Current_RTB2_B38B_Week"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'B38B',
        idb: '1',
        idc: 'current_week'
    },
    {
        $elm: $("#Forecast_RTB2_B38B_Week"),
        dbId: '',
        flag: 3,
        threshold: 'none',
        ida: 'B38B',
        idb: '2',
        idc: 'current_week'
    },
    {
        $elm: $("#Planned_RTB2_B38B_Week"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B38B',
        idb: '3',
        idc: 'current_week'
    },
    {
        $elm: $("#Deviation_RTB2_B38B_Week"),
        dbId: '',
        flag: 4,
        threshold: 'none',
        ida: 'B38B',
        idb: '4',
        idc: 'current_week'
    },
    {
        $elm: $("#Target_RTB2_B48A_Today"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B48A',
        idb: '0',
        idc: 'today'
    },
    {
        $elm: $("#Current_RTB2_B48A_Today"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'B48A',
        idb: '1',
        idc: 'today'
    },
    {
        $elm: $("#Forecast_RTB2_B48A_Today"),
        dbId: '',
        flag: 3,
        threshold: 'none',
        ida: 'B48A',
        idb: '2',
        idc: 'today'
    },
    {
        $elm: $("#Planned_RTB2_B48A_Today"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B48A',
        idb: '3',
        idc: 'today'
    },
    {
        $elm: $("#Deviation_RTB2_B48A_Today"),
        dbId: '',
        flag: 4,
        threshold: 'none',
        ida: 'B48A',
        idb: '4',
        idc: 'today'
    },
    {
        $elm: $("#Target_RTB2_B48A_Week"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B48A',
        idb: '0',
        idc: 'current_week'
    },
    {
        $elm: $("#Current_RTB2_B48A_Week"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'B48A',
        idb: '1',
        idc: 'current_week'
    },
    {
        $elm: $("#Forecast_RTB2_B48A_Week"),
        dbId: '',
        flag: 3,
        threshold: 'none',
        ida: 'B48A',
        idb: '2',
        idc: 'current_week'
    },
    {
        $elm: $("#Planned_RTB2_B48A_Week"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B48A',
        idb: '3',
        idc: 'current_week'
    },
    {
        $elm: $("#Deviation_RTB2_B48A_Week"),
        dbId: '',
        flag: 4,
        threshold: 'none',
        ida: 'B48A',
        idb: '4',
        idc: 'current_week'
    },
    {
        $elm: $("#Target_RTB2_B48B_Today"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B48B',
        idb: '0',
        idc: 'today'
    },
    {
        $elm: $("#Current_RTB2_B48B_Today"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'B48B',
        idb: '1',
        idc: 'today'
    },
    {
        $elm: $("#Forecast_RTB2_B48B_Today"),
        dbId: '',
        flag: 3,
        threshold: 'none',
        ida: 'B48B',
        idb: '2',
        idc: 'today'
    },
    {
        $elm: $("#Planned_RTB2_B48B_Today"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B48B',
        idb: '3',
        idc: 'today'
    },
    {
        $elm: $("#Deviation_RTB2_B48B_Today"),
        dbId: '',
        flag: 4,
        threshold: 'none',
        ida: 'B48B',
        idb: '4',
        idc: 'today'
    },
    {
        $elm: $("#Target_RTB2_B48B_Week"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B48B',
        idb: '0',
        idc: 'current_week'
    },
    {
        $elm: $("#Current_RTB2_B48B_Week"),
        dbId: '',
        flag: 2,
        threshold: 'none',
        ida: 'B48B',
        idb: '1',
        idc: 'current_week'
    },
    {
        $elm: $("#Forecast_RTB2_B48B_Week"),
        dbId: '',
        flag: 3,
        threshold: 'none',
        ida: 'B48B',
        idb: '2',
        idc: 'current_week'
    },
    {
        $elm: $("#Planned_RTB2_B48B_Week"),
        dbId: '',
        flag: 1,
        threshold: 'none',
        ida: 'B48B',
        idb: '3',
        idc: 'current_week'
    },
    {
        $elm: $("#Deviation_RTB2_B48B_Week"),
        dbId: '',
        flag: 4,
        threshold: 'none',
        ida: 'B48B',
        idb: '4',
        idc: 'current_week'
    }
];
var libFoundryCasting = {
    Total: [
        {
            $elm: $(".Casting_Shift1"),
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 1
        },
        {
            $elm: $("#Target_Casting_Total_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '0',
            idc: 'shift_1'
        },
        {
            $elm: $("#Current_Casting_Total_Shift1"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'Total',
            idb: '1',
            idc: 'shift_1'
        },
        {
            $elm: $("#Forecast_Casting_Total_Shift1"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'Total',
            idb: '2',
            idc: 'shift_1'
        },
        {
            $elm: $("#Planned_Casting_Total_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '3',
            idc: 'shift_1'
        },
        {
            $elm: $("#Deviation_Casting_Total_Shift1"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'Total',
            idb: '4',
            idc: 'shift_1'
        },
        {
            $elm: $(".Casting_Shift2"),
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 2
        },
        {
            $elm: $("#Target_Casting_Total_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '0',
            idc: 'shift_2'
        },
        {
            $elm: $("#Current_Casting_Total_Shift2"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'Total',
            idb: '1',
            idc: 'shift_2'
        },
        {
            $elm: $("#Forecast_Casting_Total_Shift2"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'Total',
            idb: '2',
            idc: 'shift_2'
        },
        {
            $elm: $("#Planned_Casting_Total_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '3',
            idc: 'shift_2'
        },
        {
            $elm: $("#Deviation_Casting_Total_Shift2"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'Total',
            idb: '4',
            idc: 'shift_2'
        },
        {
            $elm: $(".Casting_Shift3"),
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 3
        },
        {
            $elm: $("#Target_Casting_Total_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '0',
            idc: 'shift_3'
        },
        {
            $elm: $("#Current_Casting_Total_Shift3"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'Total',
            idb: '1',
            idc: 'shift_3'
        },
        {
            $elm: $("#Forecast_Casting_Total_Shift3"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'Total',
            idb: '2',
            idc: 'shift_3'
        },
        {
            $elm: $("#Planned_Casting_Total_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '3',
            idc: 'shift_3'
        },
        {
            $elm: $("#Deviation_Casting_Total_Shift3"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'Total',
            idb: '4',
            idc: 'shift_3'
        },
        {
            $elm: $("#Target_Casting_Total_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '0',
            idc: 'today'
        },
        {
            $elm: $("#Current_Casting_Total_Today"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'Total',
            idb: '1',
            idc: 'today',
            ide: $('.com-castingRate')
        },
        {
            $elm: $("#Forecast_Casting_Total_Today"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'Total',
            idb: '2',
            idc: 'today'
        },
        {
            $elm: $("#Planned_Casting_Total_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '3',
            idc: 'today'
        },
        {
            $elm: $("#Deviation_Casting_Total_Today"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'Total',
            idb: '4',
            idc: 'today'
        },
        {
            $elm: $("#Target_Casting_Total_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '0',
            idc: 'current_week'
        },
        {
            $elm: $("#Current_Casting_Total_Week"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'Total',
            idb: '1',
            idc: 'current_week'
        },
        {
            $elm: $("#Forecast_Casting_Total_Week"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'Total',
            idb: '2',
            idc: 'current_week'
        },
        {
            $elm: $("#Planned_Casting_Total_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'Total',
            idb: '3',
            idc: 'current_week'
        },
        {
            $elm: $("#Deviation_Casting_Total_Week"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'Total',
            idb: '4',
            idc: 'current_week'
        }
    ],
    B38A: [
        {
            $elm: $(".Casting_Shift1"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 1
        },
        {
            $elm: $("#Target_Casting_B38A_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '0',
            idc: 'shift_1'
        },
        {
            $elm: $("#Current_Casting_B38A_Shift1"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38A',
            idb: '1',
            idc: 'shift_1'
        },
        {
            $elm: $("#Forecast_Casting_B38A_Shift1"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38A',
            idb: '2',
            idc: 'shift_1'
        },
        {
            $elm: $("#Planned_Casting_B38A_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '3',
            idc: 'shift_1'
        },
        {
            $elm: $("#Deviation_Casting_B38A_Shift1"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38A',
            idb: '4',
            idc: 'shift_1'
        },
        {
            $elm: $(".Casting_Shift2"),
            flag: 5,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 2
        },
        {
            $elm: $("#Target_Casting_B38A_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '0',
            idc: 'shift_2'
        },
        {
            $elm: $("#Current_Casting_B38A_Shift2"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38A',
            idb: '1',
            idc: 'shift_2'
        },
        {
            $elm: $("#Forecast_Casting_B38A_Shift2"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38A',
            idb: '2',
            idc: 'shift_2'
        },
        {
            $elm: $("#Planned_Casting_B38A_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '3',
            idc: 'shift_2'
        },
        {
            $elm: $("#Deviation_Casting_B38A_Shift2"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38A',
            idb: '4',
            idc: 'shift_2'
        },
        {
            $elm: $("#Casting_Shift3"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 3
        },
        {
            $elm: $("#Target_Casting_B38A_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '0',
            idc: 'shift_3'
        },
        {
            $elm: $("#Current_Casting_B38A_Shift3"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38A',
            idb: '1',
            idc: 'shift_3'
        },
        {
            $elm: $("#Forecast_Casting_B38A_Shift3"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38A',
            idb: '2',
            idc: 'shift_3'
        },
        {
            $elm: $("#Planned_Casting_B38A_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '3',
            idc: 'shift_3'
        },
        {
            $elm: $("#Deviation_Casting_B38A_Shift3"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38A',
            idb: '4',
            idc: 'shift_3'
        },
        {
            $elm: $("#Target_Casting_B38A_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '0',
            idc: 'today'
        },
        {
            $elm: $("#Current_Casting_B38A_Today"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38A',
            idb: '1',
            idc: 'today',
            ide: $('.com-castingRate')
        },
        {
            $elm: $("#Forecast_Casting_B38A_Today"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38A',
            idb: '2',
            idc: 'today'
        },
        {
            $elm: $("#Planned_Casting_B38A_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '3',
            idc: 'today'
        },
        {
            $elm: $("#Deviation_Casting_B38A_Today"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38A',
            idb: '4',
            idc: 'today'
        },
        {
            $elm: $("#Target_Casting_B38A_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '0',
            idc: 'current_week'
        },
        {
            $elm: $("#Current_Casting_B38A_Week"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38A',
            idb: '1',
            idc: 'current_week'
        },
        {
            $elm: $("#Forecast_Casting_B38A_Week"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38A',
            idb: '2',
            idc: 'current_week'
        },
        {
            $elm: $("#Planned_Casting_B38A_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38A',
            idb: '3',
            idc: 'current_week'
        },
        {
            $elm: $("#Deviation_Casting_B38A_Week"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38A',
            idb: '4',
            idc: 'current_week'
        }
    ],
    B38B: [
        {
            $elm: $(".Casting_Shift1"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 1
        },
        {
            $elm: $("#Target_Casting_B38B_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '0',
            idc: 'shift_1'
        },
        {
            $elm: $("#Current_Casting_B38B_Shift1"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38B',
            idb: '1',
            idc: 'shift_1'
        },
        {
            $elm: $("#Forecast_Casting_B38B_Shift1"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38B',
            idb: '2',
            idc: 'shift_1'
        },
        {
            $elm: $("#Planned_Casting_B38B_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '3',
            idc: 'shift_1'
        },
        {
            $elm: $("#Deviation_Casting_B38B_Shift1"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38B',
            idb: '4',
            idc: 'shift_1'
        },
        {
            $elm: $(".Casting_Shift2"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 1
        },
        {
            $elm: $("#Target_Casting_B38B_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '0',
            idc: 'shift_2'
        },
        {
            $elm: $("#Current_Casting_B38B_Shift2"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38B',
            idb: '1',
            idc: 'shift_2'
        },
        {
            $elm: $("#Forecast_Casting_B38B_Shift2"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38B',
            idb: '2',
            idc: 'shift_2'
        },
        {
            $elm: $("#Planned_Casting_B38B_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '3',
            idc: 'shift_2'
        },
        {
            $elm: $("#Deviation_Casting_B38B_Shift2"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38B',
            idb: '4',
            idc: 'shift_2'
        },
        {
            $elm: $(".Casting_Shift3"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 1
        },
        {
            $elm: $("#Target_Casting_B38B_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '0',
            idc: 'shift_3'
        },
        {
            $elm: $("#Current_Casting_B38B_Shift3"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38B',
            idb: '1',
            idc: 'shift_3'
        },
        {
            $elm: $("#Forecast_Casting_B38B_Shift3"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38B',
            idb: '2',
            idc: 'shift_3'
        },
        {
            $elm: $("#Planned_Casting_B38B_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '3',
            idc: 'shift_3'
        },
        {
            $elm: $("#Deviation_Casting_B38B_Shift3"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38B',
            idb: '4',
            idc: 'shift_3'
        },
        {
            $elm: $("#Target_Casting_B38B_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '0',
            idc: 'today'
        },
        {
            $elm: $("#Current_Casting_B38B_Today"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38B',
            idb: '1',
            idc: 'today',
            ide: $('.com-castingRate')
        },
        {
            $elm: $("#Forecast_Casting_B38B_Today"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38B',
            idb: '2',
            idc: 'today'
        },
        {
            $elm: $("#Planned_Casting_B38B_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '3',
            idc: 'today'
        },
        {
            $elm: $("#Deviation_Casting_B38B_Today"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38B',
            idb: '4',
            idc: 'today'
        },
        {
            $elm: $("#Target_Casting_B38B_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '0',
            idc: 'current_week'
        },
        {
            $elm: $("#Current_Casting_B38B_Week"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B38B',
            idb: '1',
            idc: 'current_week'
        },
        {
            $elm: $("#Forecast_Casting_B38B_Week"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B38B',
            idb: '2',
            idc: 'current_week'
        },
        {
            $elm: $("#Planned_Casting_B38B_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B38B',
            idb: '3',
            idc: 'current_week'
        },
        {
            $elm: $("#Deviation_Casting_B38B_Week"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B38B',
            idb: '4',
            idc: 'current_week'
        }
    ],
    B48A: [
        {
            $elm: $(".Casting_Shift1"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 1
        },
        {
            $elm: $("#Target_Casting_B48A_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '0',
            idc: 'shift_1'
        },
        {
            $elm: $("#Current_Casting_B48A_Shift1"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48A',
            idb: '1',
            idc: 'shift_1'
        },
        {
            $elm: $("#Forecast_Casting_B48A_Shift1"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48A',
            idb: '2',
            idc: 'shift_1'
        },
        {
            $elm: $("#Planned_Casting_B48A_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '3',
            idc: 'shift_1'
        },
        {
            $elm: $("#Deviation_Casting_B48A_Shift1"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48A',
            idb: '4',
            idc: 'shift_1'
        },
        {
            $elm: $(".Casting_Shift2"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 2
        },
        {
            $elm: $("#Target_Casting_B48A_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '0',
            idc: 'shift_2'
        },
        {
            $elm: $("#Current_Casting_B48A_Shift2"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48A',
            idb: '1',
            idc: 'shift_2'
        },
        {
            $elm: $("#Forecast_Casting_B48A_Shift2"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48A',
            idb: '2',
            idc: 'shift_2'
        },
        {
            $elm: $("#Planned_Casting_B48A_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '3',
            idc: 'shift_2'
        },
        {
            $elm: $("#Deviation_Casting_B48A_Shift2"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48A',
            idb: '4',
            idc: 'shift_2'
        },
        {
            $elm: $(".Casting_Shift3"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 3
        },
        {
            $elm: $("#Target_Casting_B48A_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '0',
            idc: 'shift_3'
        },
        {
            $elm: $("#Current_Casting_B48A_Shift3"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48A',
            idb: '1',
            idc: 'shift_3'
        },
        {
            $elm: $("#Forecast_Casting_B48A_Shift3"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48A',
            idb: '2',
            idc: 'shift_3'
        },
        {
            $elm: $("#Planned_Casting_B48A_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '3',
            idc: 'shift_3'
        },
        {
            $elm: $("#Deviation_Casting_B48A_Shift3"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48A',
            idb: '4',
            idc: 'shift_3'
        },
        {
            $elm: $("#Target_Casting_B48A_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '0',
            idc: 'today'
        },
        {
            $elm: $("#Current_Casting_B48A_Today"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48A',
            idb: '1',
            idc: 'today',
            ide: $('.com-castingRate')
        },
        {
            $elm: $("#Forecast_Casting_B48A_Today"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48A',
            idb: '2',
            idc: 'today'
        },
        {
            $elm: $("#Planned_Casting_B48A_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '3',
            idc: 'today'
        },
        {
            $elm: $("#Deviation_Casting_B48A_Today"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48A',
            idb: '4',
            idc: 'today'
        },
        {
            $elm: $("#Target_Casting_B48A_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '0',
            idc: 'current_week'
        },
        {
            $elm: $("#Current_Casting_B48A_Week"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48A',
            idb: '1',
            idc: 'current_week'
        },
        {
            $elm: $("#Forecast_Casting_B48A_Week"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48A',
            idb: '2',
            idc: 'current_week'
        },
        {
            $elm: $("#Planned_Casting_B48A_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48A',
            idb: '3',
            idc: 'current_week'
        },
        {
            $elm: $("#Deviation_Casting_B48A_Week"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48A',
            idb: '4',
            idc: 'current_week'
        }
    ],
    B48B: [
        {
            $elm: $(".Casting_Shift1"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 1
        },
        {
            $elm: $("#Target_Casting_B48B_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '0',
            idc: 'shift_1'
        },
        {
            $elm: $("#Current_Casting_B48B_Shift1"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48B',
            idb: '1',
            idc: 'shift_1'
        },
        {
            $elm: $("#Forecast_Casting_B48B_Shift1"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48B',
            idb: '2',
            idc: 'shift_1'
        },
        {
            $elm: $("#Planned_Casting_B48B_Shift1"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '3',
            idc: 'shift_1'
        },
        {
            $elm: $("#Deviation_Casting_B48B_Shift1"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48B',
            idb: '4',
            idc: 'shift_1'
        },
        {
            $elm: $(".Casting_Shift2"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 2
        },
        {
            $elm: $("#Target_Casting_B48B_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '0',
            idc: 'shift_2'
        },
        {
            $elm: $("#Current_Casting_B48B_Shift2"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48B',
            idb: '1',
            idc: 'shift_2'
        },
        {
            $elm: $("#Forecast_Casting_B48B_Shift2"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48B',
            idb: '2',
            idc: 'shift_2'
        },
        {
            $elm: $("#Planned_Casting_B48B_Shift2"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '3',
            idc: 'shift_2'
        },
        {
            $elm: $("#Deviation_Casting_B48B_Shift2"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48B',
            idb: '4',
            idc: 'shift_2'
        },
        {
            $elm: $(".Casting_Shift3"),
            flag: 6,
            time_from: 'time_from',
            time_to: 'time_to',
            shift: 3
        },
        {
            $elm: $("#Target_Casting_B48B_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '0',
            idc: 'shift_3'
        },
        {
            $elm: $("#Current_Casting_B48B_Shift3"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48B',
            idb: '1',
            idc: 'shift_3'
        },
        {
            $elm: $("#Forecast_Casting_B48B_Shift3"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48B',
            idb: '2',
            idc: 'shift_3'
        },
        {
            $elm: $("#Planned_Casting_B48B_Shift3"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '3',
            idc: 'shift_3'
        },
        {
            $elm: $("#Deviation_Casting_B48B_Shift3"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48B',
            idb: '4',
            idc: 'shift_3'
        },
        {
            $elm: $("#Target_Casting_B48B_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '0',
            idc: 'today'
        },
        {
            $elm: $("#Current_Casting_B48B_Today"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48B',
            idb: '1',
            idc: 'today',
            ide: $('.com-castingRate')
        },
        {
            $elm: $("#Forecast_Casting_B48B_Today"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48B',
            idb: '2',
            idc: 'today'
        },
        {
            $elm: $("#Planned_Casting_B48B_Today"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '3',
            idc: 'today'
        },
        {
            $elm: $("#Deviation_Casting_B48B_Today"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48B',
            idb: '4',
            idc: 'today'
        },
        {
            $elm: $("#Target_Casting_B48B_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '0',
            idc: 'current_week'
        },
        {
            $elm: $("#Current_Casting_B48B_Week"),
            dbId: '',
            flag: 2,
            threshold: 'none',
            ida: 'B48B',
            idb: '1',
            idc: 'current_week'
        },
        {
            $elm: $("#Forecast_Casting_B48B_Week"),
            dbId: '',
            flag: 3,
            threshold: 'none',
            ida: 'B48B',
            idb: '2',
            idc: 'current_week'
        },
        {
            $elm: $("#Planned_Casting_B48B_Week"),
            dbId: '',
            flag: 1,
            threshold: 'none',
            ida: 'B48B',
            idb: '3',
            idc: 'current_week'
        },
        {
            $elm: $("#Deviation_Casting_B48B_Week"),
            dbId: '',
            flag: 4,
            threshold: 'none',
            ida: 'B48B',
            idb: '4',
            idc: 'current_week'
        }
    ]
};

//定义跳转标记
var urlMark = [
    {
        tabID: '#Assembly',
        $tabID: $('#tab_Assembly'),
        mark: '#Assembly',
        $tableID: $('#table_Assembly')
    },
    {
        tabID: '#ta_Machining_G6_Shift1',
        $tabID: $('#ta_Machining_G6_Shift1'),
        mark: '#Machining_G6_Shift1',
        $tableID: $('#table_Machining_G6_Shift1')
    },
    {
        tabID: '#ta_Machining_G6_Shift2',
        $tabID: $('#ta_Machining_G6_Shift2'),
        mark: '#Machining_G6_Shift2',
        $tableID: $('#table_Machining_G6_Shift2')
    },
    {
        tabID: '#ta_Machining_G6_Shift3',
        $tabID: $('#ta_Machining_G6_Shift3'),
        mark: '#Machining_G6_Shift3',
        $tableID: $('#table_Machining_G6_Shift3')
    },
    {
        tabID: '#ta_Machining_Z6_Shift1',
        $tabID: $('#ta_Machining_Z6_Shift1'),
        mark: '#Machining_Z6_Shift1',
        $tableID: $('#table_Machining_Z6_Shift1')
    },
    {
        tabID: '#ta_Machining_Z6_Shift2',
        $tabID: $('#ta_Machining_Z6_Shift2'),
        mark: '#Machining_Z6_Shift2',
        $tableID: $('#table_Machining_Z6_Shift2')
    },
    {
        tabID: '#ta_Machining_Z6_Shift3',
        $tabID: $('#ta_Machining_Z6_Shift3'),
        mark: '#Machining_Z6_Shift3',
        $tableID: $('#table_Machining_Z6_Shift3')
    },
    {
        tabID: '#ta_Foundry_RTB2_Total',
        $tabID: $('#ta_Foundry_RTB2_Total'),
        mark: '#Foundry_RTB2_Total',
        $tableID: $('#table_Foundry_RTB2_Total')
    },
    {
        tabID: '#ta_Foundry_RTB2_B38A',
        $tabID: $('#ta_Foundry_RTB2_B38A'),
        mark: '#Foundry_RTB2_B38A',
        $tableID: $('#table_Foundry_RTB2_B38A')
    },
    {
        tabID: '#ta_Foundry_RTB2_B38B',
        $tabID: $('#ta_Foundry_RTB2_B38B'),
        mark: '#Foundry_RTB2_B38B',
        $tableID: $('#table_Foundry_RTB2_B38B')
    },
    {
        tabID: '#ta_Foundry_RTB2_B48A',
        $tabID: $('#ta_Foundry_RTB2_B48A'),
        mark: '#Foundry_RTB2_B48A',
        $tableID: $('#table_Foundry_RTB2_B48A')
    },
    {
        tabID: '#ta_Foundry_RTB2_B48B',
        $tabID: $('#ta_Foundry_RTB2_B48B'),
        mark: '#Foundry_RTB2_B48B',
        $tableID: $('#table_Foundry_RTB2_B48B')
    },
    {
        tabID: '#ta_Foundry_Casting_Total',
        $tabID: $('#ta_Foundry_Casting_Total'),
        mark: '#Foundry_Casting_Total',
        $tableID: $('#table_Foundry_Casting_Total')
    },
    {
        tabID: '#ta_Foundry_Casting_B38A',
        $tabID: $('#ta_Foundry_Casting_B38A'),
        mark: '#Foundry_Casting_B38A',
        $tableID: $('#table_Foundry_Casting_B38A')
    },
    {
        tabID: '#ta_Foundry_Casting_B38B',
        $tabID: $('#ta_Foundry_Casting_B38B'),
        mark: '#Foundry_Casting_B38B',
        $tableID: $('#table_Foundry_Casting_B38B')
    },
    {
        tabID: '#ta_Foundry_Casting_B48A',
        $tabID: $('#ta_Foundry_Casting_B48A'),
        mark: '#Foundry_Casting_B48A',
        $tableID: $('#table_Foundry_Casting_B48A')
    },
    {
        tabID: '#ta_Foundry_Casting_B48B',
        $tabID: $('#ta_Foundry_Casting_B48B'),
        mark: '#Foundry_Casting_B48B',
        $tableID: $('#table_Foundry_Casting_B48B')
    }
];

//判断值是否存在
var judgeValueExist = function (res, k1, k2, k3) {
    var result = false;
    if (res && res[k1] && res[k1][k2] && is_numberic(res[k1][k2][k3]) && typeof res[k1][k2][k3] === 'number') {
        result = true;
    }
    return result;
};

//判断背景颜色
var judgeBackgroundColor = function (result, threshold, item) {
    var $elm = item.$elm;
    if (threshold !== 'none') {
        if (result < threshold) {
            $elm.css({
                'background': '#FF2700',
                'color': '#fff'
            });
        } else if (result >= threshold) {
            $elm.css({
                'background': '#008000',
                'color': '#fff'
            });
        }
    }
};


//
//判断HVB的背景颜色
var judgeHVBColor = function (deviation, threshold, item) {
    var $elm = item.$elm;
    if (threshold !== 'none') {
        if (deviation < threshold) {
            $elm.css({
                'background': '#FF2700',
                'color': '#fff'
            });
        }
        else if (deviation >= threshold && deviation < 15) {
            $elm.css({
                'background': '#008000',
                'color': '#fff'
            });
        } else {
            $elm.css({
                'background': '#FFD700',
                'color': '#fff'
            });
        }
    }
};


// 20180223,Wang Zhe BBT-A-451提需求，将assembly的背景变化方式与hvb调整成一致。
var judgeAssemblyColor = judgeHVBColor;

//进行数据处理并输出
// 数据说明：
// target与current从database中获取，
// forecast为预测完成数值，
// planned为当前时间应完成数，current/全天时间*当前时间所得
// deviation为差值，deviation=current-planned

var getAssemblyValue = function (res, item) {
    var result = 0,
        dbId = item.dbId,
        flag = item.flag,
        ida = item.ida,
        idb = item.idb,
        // weekNum = res['week_num'],
        valA = res[ida],
        valB = res[idb],
        value = res[dbId];
// //LineP单双周判断，case3，case4为早班值，case5，case6为下午班的值
// //LinePJudgement函数进行单双周P线判断，如果parm为1，LineP双周上午有值，单周下午有值；
// // 如果parm为其他，LineP双周下午有值，单周上午有值。
// //     console.log(weekNum);
//     var LinePJudgement = function (parm) {
//         if (parm === 1) {
//             if (weekNum >= 0 && weekNum % 2 === 0) {
//                 switch (flag) {
//                     case 3:
//                         if (typeof value === 'number' && value >= 0) {
//                             result = parseInt(value);
//                         } else {
//                             result = "--";
//                         }
//                         break;
//                     case 4:
//                         if (typeof value === 'number' && value >= 0) {
//                             result = parseInt(value);
//                             var deviation = valA - valB;
//                             judgeAssemblyColor(deviation, 0, item);
//                         } else {
//                             result = "--";
//                         }
//                         break;
//                     case 5:
//                         result = '--';
//                         break;
//                     case 6:
//                         result = '--';
//                         break;
//                 }
//             }
//             if (weekNum >= 0 && weekNum % 2 === 1) {
//                 switch (flag) {
//                     case 5:
//                         if (typeof value === 'number' && value >= 0) {
//                             result = parseInt(value);
//                         } else {
//                             result = "--";
//                         }
//                         break;
//                     case 6:
//                         if (typeof value === 'number' && value >= 0) {
//                             result = parseInt(value);
//                             deviation = valA - valB;
//                             judgeAssemblyColor(deviation, 0, item);
//                         } else {
//                             result = "--";
//                         }
//                         break;
//                     case 3:
//                         result = '--';
//                         break;
//                     case 4:
//                         result = '--';
//                         break;
//                 }
//             }
//         } else {
//             if (weekNum >= 0 && weekNum % 2 === 0) {
//                 switch (flag) {
//                     case 5:
//                         if (typeof value === 'number' && value >= 0) {
//                             result = parseInt(value);
//                         } else {
//                             result = "--";
//                         }
//                         break;
//                     case 6:
//                         if (typeof value === 'number' && value >= 0) {
//                             result = parseInt(value);
//                             deviation = valA - valB;
//                             judgeAssemblyColor(deviation, 0, item);
//                         } else {
//                             result = "--";
//                         }
//                         break;
//                     case 3:
//                         result = '--';
//                         break;
//                     case 4:
//                         result = '--';
//                         break;
//                 }
//             }
//             if (weekNum >= 0 && weekNum % 2 === 1) {
//                 switch (flag) {
//                     case 3:
//                         if (typeof value === 'number' && value >= 0) {
//                             result = parseInt(value);
//                         } else {
//                             result = "--";
//                         }
//                         break;
//                     case 4:
//                         if (typeof value === 'number' && value >= 0) {
//                             result = parseInt(value);
//                             deviation = valA - valB;
//                             judgeAssemblyColor(deviation, 0, item);
//                         } else {
//                             result = "--";
//                         }
//                         break;
//                     case 5:
//                         result = '--';
//                         break;
//                     case 6:
//                         result = '--';
//                         break;
//                 }
//             }
//         }
//     };
//     LinePJudgement(2);
//     //第52周特殊情况
//     // if (weekNum && weekNum === 52) {
//     //     switch (flag) {
//     //         case 3:
//     //             if (value && typeof value === 'number') {
//     //                 result = parseInt(value);
//     //             } else {
//     //                 result = "--";
//     //             }
//     //             break;
//     //         case 4:
//     //             if (value && typeof value === 'number') {
//     //                 result = parseInt(value);
//     //                 deviation = valA - valB;
//     //                 judgeBackgroundColor(deviation, 0, item);
//     //             } else {
//     //                 result = "--";
//     //             }
//     //             break;
//     //         case 5:
//     //             result = '--';
//     //             break;
//     //         case 6:
//     //             result = '--';
//     //             break;
//     //     }
//     // }

    switch (flag) {
        case 1:
            if (typeof value === 'number' && value >= 0) {
                result = parseInt(value);
            } else {
                result = "--";
            }
            break;
        case 2:
            if (typeof value === 'number' && value >= 0) {
                result = parseInt(value);
                var deviation = valA - valB;
                judgeAssemblyColor(deviation, 0, item);
            } else {
                result = "--";
            }
            break;
    }
    return result;
};
var getHVBValue = function (res, item) {
    var result = 0,
        dbId = item.dbId,
        flag = item.flag,
        ida = item.ida,
        idb = item.idb,
        weekNum = res['week_num'],
        valA = res[ida],
        valB = res[idb],
        value = res[dbId];

    switch (flag) {
        case 1:
            if (is_numberic(value)) {
                result = parseInt(value);
            } else {
                result = "--";
            }
            break;
        case 2:
            if (is_numberic(value)) {
                result = parseInt(value);
                var deviation = valA - valB;
                judgeHVBColor(deviation, 0, item);
            } else {
                result = "--";
            }
            break;
        // case 3:
        //     console.log(ida);
        //     var startTime = ida.substring(0, 4);
        //     var endTime = ida.substring(0, 4);
        //     result = 'Shift1' + startTime + '<br>' + endTime;
        //     break;

    }
    return result;
};
var getG6Value = function (res, item) {
    var result = 0,
        dbId = item.dbId,
        flag = item.flag,
        ide = item.ide,
        valA = res[item['ida']],
        valB = res[item['idb']],
        value = res[dbId];

    switch (flag) {
        case 1:
            //直接从API中取target和planned的值
            if (value && typeof value === 'number') {
                result = parseInt(value);
            } else {
                result = "--";
            }
            break;
        case 2:
            //计算deviation的值（current - planned）并修改背景色
            if (valA && typeof valA === 'number' && valB && typeof valB === 'number') {
                result = valA - valB;
                judgeBackgroundColor(result, 0, item);
            } else if (!valA && valB && typeof valB === 'number') {
                // 如果current产量值不存在，则deviation为负值
                result = -valB;
                judgeBackgroundColor(result, 0, item);
            }
            else {
                result = "--";
            }
            break;
        case 3:
            //从API中获取current和forecast的值，计算阈值并修改背景色，current的阈值是planned，forecast的阈值是target
            if (valA && typeof valA === 'number' && valB && typeof valB === 'number') {
                result = parseInt(value);
                var thresholdMachining = valA - valB;
                judgeBackgroundColor(thresholdMachining, 0, item);
            } else {
                result = "--";
            }
            if (ide && ide.length) {

                //如果有ide，进行rate计算
                calcLineRateMachining(result, dbId, ide, res);
            }
            break;
    }
    return result;
};
var getZ6Value = function (res, item) {
    var result = 0,
        dbId = item.dbId,
        flag = item.flag,
        ide = item.ide,
        valA = res[item['ida']],
        valB = res[item['idb']],
        value = res[dbId];
    switch (flag) {
        case 1:
            //从API中获取target和planned的值
            if (value && typeof value === 'number') {
                result = parseInt(value);
            } else {
                result = "--";
            }
            break;
        case 2:
            //计算deviation的值（current - planned）并修改背景色
            if (valA && typeof valA === 'number' && valB && typeof valB === 'number') {
                result = valA - valB;
                judgeBackgroundColor(result, 0, item);
            } else if (!valA && valB && typeof valB === 'number') {
                // 如果current产量值不存在，则deviation为负值
                result = -valB;
                judgeBackgroundColor(result, 0, item);
            }
            else {
                result = "--";
            }
            break;
        case 3:
            //从API中获取current和forecast的值，计算阈值并修改背景色，current的阈值是planned，forecast的阈值是target
            if (valA && typeof valA === 'number' && valB && typeof valB === 'number') {
                result = parseInt(value);
                var thresholdMachining = valA - valB;
                judgeBackgroundColor(thresholdMachining, 0, item);
            } else {
                result = "--";
            }
            if (ide) {
                //如果有ide，进行rate计算
                calcLineRateMachining(result, dbId, ide, res);
            }
            break;

    }
    return result;
};
var getRTB2Value = function (res, item) {
    var result = 0,
        flag = item.flag,
        ida = item.ida,
        idb = item.idb,
        idc = item.idc,
        ide = item.ide;
    switch (flag) {
        case 1:
            //从API中获取非total的target的值
            if (judgeValueExist(res, ida, idb, idc)) {
                result = res[ida][idb][idc];
            } else {
                result = "--";
            }
            break;
        case 2:
            //从API中获取非total的current的值，并根据相应planned的值进行背景色判断
            if (judgeValueExist(res, ida, idb, idc)) {
                result = res[ida][idb][idc];
                var thresholdCurrent = res[ida][3][idc];
                judgeBackgroundColor(result, thresholdCurrent, item);
            } else {
                result = "--";
            }
            //计算全部current的值并传入rate函数进行计算
            var sumResult = res['B38A'][1]['today'] + res['B38B'][1]['today'] + res['B48A'][1]['today'] + res['B48B'][1]['today'];
            if (ide) {
                calcLineRateFoundry(sumResult, ide);
            }
            break;
        case 3:
            //从API中获取非total的forecast的值，并根据相应target的值进行背景色判断
            if (judgeValueExist(res, ida, idb, idc)) {
                result = res[ida][idb][idc];
                var thresholdForecast = res[ida][0][idc];
                judgeBackgroundColor(result, thresholdForecast, item);
            } else {
                result = "--";
            }
            break;
        case 4:
            //从API中获取非total的deviation的值，并进行背景色判断
            if (judgeValueExist(res, ida, idb, idc)) {
                result = res[ida][idb][idc];
                judgeBackgroundColor(result, 0, item);
            } else {
                result = "--";
            }
            break;
        case 5:
            //total值计算
            var tValue;
            // console.log(res);
            // console.log(res['B38A'][idb][idc]);
            // console.log(res['B38B'][idb][idc]);
            // console.log(res['B48A'][idb][idc]);
            // console.log(res['B48B'][idb][idc]);
            if (judgeValueExist(res, 'B38A', idb, idc)) {
                //计算全部求和值
                result = res['B38A'][idb][idc] + res['B38B'][idb][idc] + res['B48A'][idb][idc] + res['B48B'][idb][idc];

            } else {
                result = '--';
            }
            if (idb === '1') {
                //计算current阈值（planned）并进行背景颜色更改
                if (judgeValueExist(res, 'B38A', 3, idc)) {
                    tValue = res['B38A'][3][idc] + res['B38B'][3][idc] + res['B48A'][3][idc] + res['B48B'][3][idc];
                    judgeBackgroundColor(result, tValue, item);
                }
            }
            if (idb === '2') {
                //计算forecast阈值（target）并进行背景颜色更改
                if (judgeValueExist(res, 'B38A', 0, idc)) {
                    tValue = res['B38A'][0][idc] + res['B38B'][0][idc] + res['B48A'][0][idc] + res['B48B'][0][idc];
                }
                judgeBackgroundColor(result, tValue, item);
            }
            if (idb === '4') {
                //计算deviation的阈值并进行背景颜色更改
                judgeBackgroundColor(result, 0, item);
            }
            if (ide) {
                //进行rate计算
                calcLineRateFoundry(result, ide);
            }
            break;
    }
    return result;
};
var getCastingValue = function (res, index, item) {
    var result = 0,
        flag = item.flag,
        ida = item.ida,
        idb = item.idb,
        idc = item.idc,
        ide = item.ide,
        nowDate = new Date(),
        nowTime = nowDate.getTime(),
        startTime = nowDate.setHours(6, 0, 0, 0),
        passTime;
    if (nowDate >= startTime) {
        passTime = nowTime - startTime;

    } else {
        passTime = nowTime - startTime + 86400000;
    }
    if (index !== 'Total') {
        //对非total的值进行计算
        switch (flag) {
            case 1:
                //从API中获取非total的target的值
                if (judgeValueExist(res, ida, idb, idc)) {
                    result = res[ida][idb][idc];
                } else {
                    result = "--";
                }
                break;
            case 2:
                //从API中获取非total的current的值，并根据相应planned的值进行背景色判断
                if (judgeValueExist(res, ida, idb, idc)) {
                    result = res[ida][idb][idc];
                    var thresholdCurrent = res[ida][3][idc];
                    judgeBackgroundColor(result, thresholdCurrent, item);
                } else {
                    result = "--";
                }
                //计算总的current的值并将结果传进函数进行rate的计算
                var sumResult = res['B38A'][1]['today'] + res['B38B'][1]['today'] + res['B48A'][1]['today'] + res['B48B'][1]['today'];
                if (ide) {
                    calcLineRateFoundry(sumResult, ide);
                }
                break;
            case 3:
                //从API中获取非total的forecast的值，并根据相应target的值进行背景色判断
                if (judgeValueExist(res, ida, idb, idc)) {
                    result = res[ida][idb][idc];
                    var thresholdForecast = res[ida][0][idc];
                    judgeBackgroundColor(result, thresholdForecast, item);
                } else {
                    result = "--";
                }
                break;
            case 4:
                //从API中获取非total的deviation的值，并进行背景色判断
                if (judgeValueExist(res, ida, idb, idc)) {
                    result = res[ida][idb][idc];
                    judgeBackgroundColor(result, 0, item);
                } else {
                    result = "--";
                }
                break;
            // case 5:
            //     //单独计算today的forecast值，三班产量总和/当前毫秒数再*全体的毫秒数
            //     //首先判断日期是否是今天，是则进行计算，不是则从API中调取数据，阈值是today的target值
            //     if (dateTargetFoundry === 0 || dateTargetFoundry === getTime()) {
            //         if (judgeValueExist(res, ida, idb, 'shift_1') && judgeValueExist(res, ida, idb, 'shift_2') && judgeValueExist(res, ida, idb, 'shift_3')) {
            //             var currentValue = res[ida][idb]['shift_1'] + res[ida][idb]['shift_2'] + res[ida][idb]['shift_3'];
            //             result = parseInt(currentValue * 86400000 / passTime);
            //         } else if (judgeValueExist(res, ida, idb, 'shift_1') && judgeValueExist(res, ida, idb, 'shift_2') && !judgeValueExist(res, ida, idb, 'shift_3')) {
            //             currentValue = res[ida][idb]['shift_1'] + res[ida][idb]['shift_2'];
            //             result = parseInt(currentValue * 86400000 / passTime);
            //         } else if (judgeValueExist(res, ida, idb, 'shift_1') && !judgeValueExist(res, ida, idb, 'shift_2')) {
            //             currentValue = res[ida][idb]['shift_1'];
            //             result = parseInt(currentValue * 86400000 / passTime);
            //         } else {
            //             result = '--';
            //         }
            //         if (judgeValueExist(res, ida, 0, idc)) {
            //             thresholdForecast = res[ida][0][idc];
            //         }
            //         judgeBackgroundColor(result, thresholdForecast, item);
            //     } else {
            //         if (judgeValueExist(res, ida, 0, idc)) {
            //             thresholdForecast = res[ida][0][idc];
            //         }
            //         if (judgeValueExist(res, ida, idb, idc)) {
            //             result = res[ida][idb][idc];
            //             judgeBackgroundColor(result, thresholdForecast, item);
            //         } else {
            //             result = "--";
            //         }
            //     }
            //     break;
        }
    }
    if (index === 'Total') {
        //对total值进行计算
        try {
            if (!is_numberic(res['B38A'][idb][idc])) {
                res['B38A'][idb][idc] = 0;
            }
            if (!is_numberic(res['B38B'][idb][idc])) {
                res['B38B'][idb][idc] = 0;
            }
            if (!is_numberic(res['B48A'][idb][idc])) {
                res['B48A'][idb][idc] = 0;
            }
            if (!is_numberic(res['B48B'][idb][idc])) {
                res['B48B'][idb][idc] = 0;
            }
        } catch (e) {
            console.log(e)
        }
        if (flag) {
            //计算全部求和值
            var tValue;
            try {
                result = res['B38A'][idb][idc] + res['B38B'][idb][idc] + res['B48A'][idb][idc] + res['B48B'][idb][idc];
            }
            catch (err) {
                result = 0
            }
            if (!result) {
                result = '--';
            }
        }
        if (idb === '1') {
            //计算current的阈值(planned)，并进行背景色修改
            try {
                tValue = res['B38A'][3][idc] + res['B38B'][3][idc] + res['B48A'][3][idc] + res['B48B'][3][idc];
                judgeBackgroundColor(result, tValue, item);
            } catch (e) {
                console.log('Current的阈值出错');
            }
        }
        if (idb === '2') {
            //计算forecast的阈值(target)，并进行背景色修改
            try {
                tValue = res['B38A'][0][idc] + res['B38B'][0][idc] + res['B48A'][0][idc] + res['B48B'][0][idc];
                judgeBackgroundColor(result, tValue, item);
            } catch (e) {
                console.log('Forecast的阈值出错');
            }
        }
        if (idb === '4') {
            //计算deviation的阈值(target)，并进行背景色修改
            judgeBackgroundColor(result, 0, item);
        }
        // if (idb === '5') {
        //     //单独计算today的forecast值，三班产量总和/当前毫秒数再*全体的毫秒数
        //     //首先判断日期是否是今天，是则进行计算，不是则从API中调取数据，阈值是today的target值
        //     if (dateTargetFoundry === 0 || dateTargetFoundry === getTime()) {
        //         var CurrentShift1 = res['B38A'][1]['shift_1'] + res['B38B'][1]['shift_1'] + res['B48A'][1]['shift_1'] + res['B48B'][1]['shift_1'],
        //             CurrentShift2 = res['B38A'][1]['shift_2'] + res['B38B'][1]['shift_2'] + res['B48A'][1]['shift_2'] + res['B48B'][1]['shift_2'],
        //             CurrentShift3 = res['B38A'][1]['shift_3'] + res['B38B'][1]['shift_3'] + res['B48A'][1]['shift_3'] + res['B48B'][1]['shift_3'];
        //         result = parseInt((CurrentShift1 + CurrentShift2 + CurrentShift3) * 86400000 / passTime);
        //         tValue = res['B38A'][0]['today'] + res['B38B'][0]['today'] + res['B48A'][0]['today'] + res['B48B'][0]['today'];
        //         if (!result) {
        //             result = '--';
        //         }
        //         judgeBackgroundColor(result, tValue, item);
        //     } else {
        //         result = res['B38A'][2][idc] + res['B38B'][2][idc] + res['B48A'][2][idc] + res['B48B'][2][idc];
        //         tValue = res['B38A'][0][idc] + res['B38B'][0][idc] + res['B48A'][0][idc] + res['B48B'][0][idc];
        //         judgeBackgroundColor(result, tValue, item);
        //     }
        // }
        if (ide) {
            calcLineRateFoundry(result, ide);
        }
    }
    return result;
};
var getCastingShift = function (index, item) {
    var result = 0,
        time_from = item.time_from,
        time_to = item.time_to,
        shift = item.shift,
        backgroundSize = $(window).width();
    if (index[shift] && index[shift][time_from] && index[shift][time_to]) {
        var timeFrom = index[shift][time_from];
        var timeTo = index[shift][time_to];
        if (backgroundSize < 450) {
            result = '<div>' + 'Shift' + shift + '</div>' + '<div>' + timeFrom.slice(11, 16) + '</div>' + '<div class="com-lineHeight">' + '-' + '</div>' + '<div>' + timeTo.slice(11, 16) + '</div>';
        } else {
            result = '<div>' + 'Shift' + shift + '</div>' + '<div>' + timeFrom.slice(11, 16) + '-' + timeTo.slice(11, 16) + '</div>';
        }
        return result;
    }

};

//进行Rate计算并输出
var calcLineRateMachining = function (data, shift, id, res) {
    // console.log('*******************');
    // console.log(data);
    // console.log(shift);
    // console.log(id);
    // console.log(res);

    var date = new Date(),
        hour = date.getHours(),
        minutes = date.getMinutes(),
        time = hour + parseInt(minutes / 6) / 10,
        sumHour,
        s1 = parseInt(res['S1']),
        e1 = parseInt(res['E1']),
        s2 = parseInt(res['S2']),
        e2 = parseInt(res['E2']),
        s3 = parseInt(res['S3']),
        e3 = parseInt(res['E3']),
        result;
    var currentDateObj = new Date();
    var currentTimestamp = parseInt(Date.parse(currentDateObj) / 1000);
    var shiftTimestamp;
    if (shift === 'CurrentOutput1' && time <= e1 && time > s1) {
        shiftTimestamp = parseInt(moment(res['StartTime1']).format('X'));
        sumHour = (currentTimestamp - shiftTimestamp) / 3600;
        console.log(sumHour);
    } else if (shift === 'CurrentOutput2' && time <= e2 && time > s2) {
        shiftTimestamp = parseInt(moment(res['StartTime2']).format('X'));
        // var shiftTimestamp = parseInt(shiftObj / 1000);
        sumHour = (currentTimestamp - shiftTimestamp) / 3600;
    } else if (shift === 'CurrentOutput3' && time <= e3 || time > s3) {
        shiftTimestamp = parseInt(moment(res['StartTime3']).format('X'));
        // var shiftTimestamp = parseInt(shiftObj / 1000);
        sumHour = (currentTimestamp - shiftTimestamp) / 3600;
    }

    // if (shift == 'CurrentInput1' && time <= e1 && time > s1) {
    //     shiftTimestamp = parseInt(moment(res['StartTime1']).format('X'));
    //     sumHour = (currentTimestamp - shiftTimestamp) / 3600;
    // } else if (shift == 'CurrentInput2' && time <= e2 && time > s2) {
    //     shiftTimestamp = parseInt(moment(res['StartTime2']).format('X'));
    //     // var shiftTimestamp = parseInt(shiftObj / 1000);
    //     sumHour = (currentTimestamp - shiftTimestamp) / 3600;
    // } else if (shift == 'CurrentInput3' && time <= e3 || time > s3) {
    //     shiftTimestamp = parseInt(moment(res['StartTime3']).format('X'));
    //     // var shiftTimestamp = parseInt(shiftObj / 1000);
    //     sumHour = (currentTimestamp - shiftTimestamp) / 3600;
    // }

    if (sumHour && data && typeof data === 'number') {
        result = 'Current line rate:' + ' ' + Math.round(data / sumHour * 10) / 10 + ' ' + 'U/H';
    } else {
        result = 'Current line rate:' + ' ' + ' -- ' + ' ' + 'U/H';
    }
    // console.log('------------------------');
    // console.log(data);
    // console.log(sumHour);
    // console.log('------------------------');
    id.html(result);
};

var calcLineRateFoundry = function (data, id) {
    // console.log(data);
    // console.log(id);
    var date = new Date(),
        y = date.getFullYear(),
        m = date.getMonth() + 1,
        d = date.getDate(),
        paddingNum = function (n) {
            if (n < 10) {
                n = '0' + n;
            }
            return n;
        };
    var tDate = y + '-' + paddingNum(m) + '-' + paddingNum(d);
    var result;
    var shift1 = y + '-' + paddingNum(m) + '-' + paddingNum(d) + ' 06:00:00';
    // console.log(tDate);
    // console.log(shift1);
    // console.log(moment(shift1).format('X'));
    // console.log(dateTargetFoundry);
    var currentDateObj = new Date();
    var currentTimestamp = parseInt(Date.parse(currentDateObj) / 1000);
    var shiftTimestamp = parseInt(moment(shift1).format('X'));
    var sumHour = (currentTimestamp - shiftTimestamp) / 3600;
    if (sumHour < 0) {
        sumHour = 24 + sumHour;
    }
    console.log('sumHour is :' + sumHour);
    if (data && typeof data === 'number') {
        if (dateTargetFoundry === tDate || dateTargetFoundry === 0) {
            result = 'Current line rate:' + ' ' + parseInt(data / sumHour * 10) / 10 + ' ' + 'U/H';
        } else {
            result = 'Current line rate:' + ' ' + parseInt(data / 24 * 10) / 10 + ' ' + 'U/H';
        }
    } else {
        result = 'Current line rate:' + ' ' + ' -- ' + ' ' + 'U/H';
    }
    id.html(result);
};

//从后端获取数据
var getAssemblyData = function () {
    $.ajax({
        url: '/NEP/ALS/api/Assembly?from_date=' + '&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libAssembly, function (index, item) {
                item.$elm.css({
                    'background': '#fff',
                    'color': '#000'
                });
                item.$elm.html(getAssemblyValue(res['assembly_shifts_info'], item));
                var shift_dict = res['shift_dict'];
                var startTime1 = shift_dict['shift_1_start_time'].substring(0, 5);
                var endTime1 = shift_dict['shift_1_end_time'].substring(0, 5);
                var shiftInfo1 = 'Shift1' + '<br>' + startTime1 + '-' + endTime1;
                var startTime2 = shift_dict['shift_2_start_time'].substring(0, 5);
                var endTime2 = shift_dict['shift_2_end_time'].substring(0, 5);
                var shiftInfo2 = 'Shift2' + '<br>' + startTime2 + '-' + endTime2;
                $('#Assembly_Shift1').html(shiftInfo1);
                $('#Assembly_Shift2').html(shiftInfo2);
            });
        }
    })
};
var getHVBData = function () {
    $.ajax({
        // url: '/NEP/ALS/api/HVB?from_date=' + '&' + Date.now(),
        url: '/NEP/ALS/api/Assembly?from_date=' + '&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libHVB, function (index, item) {
                item.$elm.css({
                    'background': '#fff',
                    'color': '#000'
                });
                console.log(res['hvb_shifts_info']);
                item.$elm.html(getHVBValue(res['hvb_shifts_info'], item));
                var shift_dict = res['shift_dict'];
                var startTime1 = shift_dict['hvb_shift_1_start_time'].substring(0, 5);
                var endTime1 = shift_dict['hvb_shift_1_end_time'].substring(0, 5);
                var shiftInfo1 = 'Shift1' + '<br>' + startTime1 + '-' + endTime1;
                var startTime2 = shift_dict['hvb_shift_2_start_time'].substring(0, 5);
                var endTime2 = shift_dict['hvb_shift_2_end_time'].substring(0, 5);
                var shiftInfo2 = 'Shift2' + '<br>' + startTime2 + '-' + endTime2;
                $('#HVB_Shift1').html(shiftInfo1);
                $('#HVB_Shift2').html(shiftInfo2);
            });
        }
    })
};
var getG6Data = function () {
    $.ajax({
        url: '/NEP/PSS/api/product_info?line_name=G6&date=' + dateTargetMachining + '&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libMachiningG6, function (index, item) {
                item.$elm.css({
                    'background': '#fff',
                    'color': '#000'
                });
                item.$elm.html(getG6Value(res, item));
            });
            if (res && res['weekLineRate']) {
                for (var i = 1; i < 4; i++) {
                    var id = 'Week_rate_Shift' + i + '_Machining_G6';
                    var $id = $('#' + id);
                    $id.html('Weekly LR: ' + res['weekLineRate'] + ' U/H')
                }
                var week = date.getDay(),
                    s1 = parseInt(res['S1']),
                    e1 = parseInt(res['E1']),
                    hour = date.getHours(),
                    minutes = date.getMinutes(),
                    time = hour + parseInt(minutes / 6) / 10;
                // console.log(week);
                // console.log('G6 weekLineRate');
                // console.log(res['weekLineRate']);
                if (time <= e1 && time > s1 && week === 1) {
                    $('#Rate_Shift1_Machining_G6').html('Current line rate:' + ' ' + res['weekLineRate'] + ' ' + 'U/H');
                }
            }
        }
    })
};
var getZ6Data = function () {
    $.ajax({
        url: '/NEP/PSS/api/product_info?line_name=Z6&date=' + dateTargetMachining + '&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libMachiningZ6, function (index, item) {
                item.$elm.css({
                    'background': '#fff',
                    'color': '#000'
                });
                item.$elm.html(getZ6Value(res, item));
            });
            if (res && res['weekLineRate']) {
                for (var i = 1; i < 4; i++) {
                    var id = 'Week_rate_Shift' + i + '_Machining_Z6';
                    var $id = $('#' + id);
                    $id.html('Weekly LR: ' + res['weekLineRate'] + ' U/H')
                }
                var week = date.getDay(),
                    s1 = parseInt(res['S1']),
                    e1 = parseInt(res['E1']),
                    hour = date.getHours(),
                    minutes = date.getMinutes(),
                    time = hour + parseInt(minutes / 6) / 10;
                // console.log(week);
                // console.log('Z6 weekLineRate');
                // console.log(res['weekLineRate']);
                if (time <= e1 && time > s1 && week === 1) {
                    $('#Rate_Shift1_Machining_Z6').html('Current line rate:' + ' ' + res['weekLineRate'] + ' ' + 'U/H');
                }
            }
        }
    })
};
var getRTB2Data = function () {
    $.ajax({
        url: '/NEP/ALS/api/engine_data?type=RTB2&from_date=' + dateTargetFoundry + '&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libFoundryRTB2, function (index, item) {
                item.$elm.css({
                    'background': '#fff',
                    'color': '#000'
                });
                item.$elm.html(getRTB2Value(res['data'], item));
            });
            if (res && res['data'] && res['data']['week_LR'] >= 0) {
                $('.__RTB_weekly_LR').html(res['data']['week_LR']);
            }
        }
    })
};
var getCastingData = function () {
    $.ajax({
        url: '/NEP/ALS/api/engine_data?from_date=' + dateTargetFoundry + '&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libFoundryCasting, function (index1, item) {
                $.each(item, function (index2, obj) {
                    obj.$elm.css({
                        'background': '#fff',
                        'color': '#000'
                    });
                    $('.Casting_Shift1').css('background', '#E1E7F0');
                    $('.Casting_Shift2').css('background', '#E1E7F0');
                    $('.Casting_Shift3').css('background', '#E1E7F0');
                    obj.$elm.html(getCastingValue(res['data'], index1, obj));
                    // console.log(res['data']);
                    obj.$elm.html(getCastingShift(res['shift_info'], obj));
                });
            });
            if (res && res['data'] && res['data']['week_LR'] >= 0) {
                $('.__cast_weekly_LR').html(res['data']['week_LR']);
            }
        }
    })
};

//获取当前时间
var getTime = function () {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    month = checkTime(month);
    day = checkTime(day);
    hour = checkTime(hour);
    minute = checkTime(minute);
    second = checkTime(second);
    if (dateTargetMachining == 0) {
        dateTargetMachining = year.toString() + month.toString() + day.toString();
    }
    if (dateTargetFoundry == 0) {
        dateTargetFoundry = year.toString() + '-' + month.toString() + '-' + day.toString();
    }
    var time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    var nowDate = year + "-" + month + "-" + day;

    function checkTime(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }

    $('.com-date').html(dateTargetFoundry);
    $('.com-time').html(time);
    setTimeout(function () {
        getTime()
    }, 500);
    return nowDate;
};

//运行函数
$(function () {
    //初始化所有数据
    setBackground();
    getTime();
    getAssemblyData();
    getHVBData();
    getG6Data();
    getZ6Data();
    getRTB2Data();
    getCastingData();
    //循环获取数据
    th = setInterval(function () {
        getAssemblyData();
        getHVBData();
        getG6Data();
        getZ6Data();
        getRTB2Data();
        getCastingData();
    }, 60000);

    $(window).on('resize', function () {
        setBackground();
    });

    $('.com-tab').on('click', function (e) {
        e.preventDefault();
        var $this = $(this);
        $('.active').removeClass('active');
        $this.tab('show');
        //去掉assembly的calculator
        if ($this.attr('id') === 'tab_Assembly') {
            $('.com-timePicker').addClass('hide');
        }
        else {
            $('.com-timePicker').removeClass('hide');
        }
    });

    $.each(urlMark, function (index, item) {
        item.$tabID.click(function () {
            window.location.href = url + item.mark;
        });
    });

//根据url进行table和calculator的跳转
    if (tabId) {
        $.each(urlMark, function (index, item) {
            if (tabId === item.mark) {
                if (item.mark === '#Assembly') {
                    $('.com-timePicker').addClass('hide');
                } else {
                    $('.com-timePicker').removeClass('hide');
                }
                $(".active").removeClass('active');
                item.$tabID.addClass('active');
                item.$tableID.addClass('active in');
            }
        });
    }
    $datePicker.datetimepicker({
        format: 'yyyy-mm-dd',
        language: 'en',
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 1,
        initialDate: new Date(),
        pickerPosition: 'bottom-left'
    });
    $datePicker.datetimepicker().on(
        'hide', function () {
            dateTargetFoundry = $('.form-control').val();
            $('.com-date').html(dateTargetFoundry);
            dateTargetMachining = dateTargetFoundry.replace(/-/g, "");
            getAssemblyData();
            getG6Data();
            getZ6Data();
            getRTB2Data();
            getCastingData();
            // console.log(data.replace(/\//g, ''));
        }
    );
});


